package mylibrary.httpclient;

import java.io.IOException;

import org.apache.commons.httpclient.DefaultHttpMethodRetryHandler;
import org.apache.commons.httpclient.HttpClient;
import org.apache.commons.httpclient.HttpException;
import org.apache.commons.httpclient.HttpStatus;
import org.apache.commons.httpclient.methods.GetMethod;
import org.apache.commons.httpclient.params.HttpMethodParams;

public class GetSample {

	public static void main(String[] args) {
		
		HttpClient httpClient = new HttpClient();
		
		GetMethod getMethod = new GetMethod("http://www.ibm.com/");
		
		getMethod.getParams().setParameter(HttpMethodParams.RETRY_HANDLER, new DefaultHttpMethodRetryHandler());
		
		try {
			int statusCode = httpClient.executeMethod(getMethod);
			
			if(statusCode != HttpStatus.SC_OK) {
				System.err.println("Method failed:" + getMethod.getStatusLine());
			}
			
			byte[] responseBody = getMethod.getResponseBody();
			System.out.println(new String(responseBody));
			
			//System.out.println(getMethod.getResponseBodyAsString());
		} catch (HttpException e) {
			e.printStackTrace();
		} catch (IOException e) {
			e.printStackTrace();
		} finally {
			getMethod.releaseConnection();
		}
		
	}
}
